package cn.t.utils;

import cn.t.constants.CommonConstants;
import org.springframework.util.StringUtils;

/**
 * 脱敏工具类
 */
public class DesensitizationUtil {

    private DesensitizationUtil() {}

    public static String generate(int prefixLen, String content, int suffixLen, int len) {
        if (!StringUtils.hasText(content) || "null".equals(content)) {
            return CommonConstants.PLACE_HOLD_BLANK;
        }

        if (prefixLen >= content.length()) {
            return content;
        }

        StringBuilder stars = new StringBuilder();
        while (len > 0) {
            stars.append("*");
            len --;
        }

        if (prefixLen + suffixLen >= content.length()) {
            return content.substring(0, prefixLen) + "*";
        } else {
            return content.substring(0, prefixLen) + stars + content.substring(content.length() - suffixLen);
        }
    }
}
